package com.mi.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.mi.common.core.util.Result;
import com.mi.common.log.annotation.Log;
import com.mi.system.dto.query.DictQueryDTO;
import com.mi.system.dto.request.DictRequestDTO;
import com.mi.system.entity.Dict;
import com.mi.system.service.DictService;
import com.mi.system.vo.DictVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static com.mi.common.core.util.Result.ok;

/**
 * Dict controller
 *
 * @author jin_huaquan
 * @version 1.0
 * @date 2021/11/1 15:06
 */
@Api(value = "字典类型API", tags = "Dict")
@ApiSort(995)
@RestController
@RequestMapping("/api/v1/dict")
@RequiredArgsConstructor
@Slf4j
public class DictController {

    private final DictService dictService;

    @ApiOperation(value = "分页查询字典类型列表", notes = "分页查询字典类型列表")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 1)
    @Log(title = "查询字典类型列表")
    @GetMapping("/actions/list")
    public Result<IPage<DictVO>> listDict(Page page, DictQueryDTO query) {
        log.info("API CALL : list dict.");

        return ok(dictService.listDict(page, query));
    }

    @ApiOperation(value = "保存字典类型", notes = "保存字典类型")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 2)
    @Log(title = "新增字典类型:#{#request.type}")
    @PostMapping
    public Result<Boolean> saveDict(@Valid @RequestBody DictRequestDTO request) {
        log.debug("API CALL : save dict.");

        return ok(dictService.saveDict(request));
    }

    @ApiOperation(value = "更新字典类型", notes = "更新字典类型信息")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 3)
    @ApiImplicitParam(name = "id", value = "字典类型的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "编辑字典类型:#{#request.type}")
    @PutMapping("/{id}")
    public Result<Boolean> updateDict(@PathVariable Long id, @Valid @RequestBody DictRequestDTO request) {
        log.debug("API CALL : update dict.");

        return ok(dictService.updateDict(id, request));
    }

    @ApiOperation(value = "删除字典类型", notes = "删除字典类型")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 4)
    @ApiImplicitParam(name = "id", value = "字典类型的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "删除字典类型:#{#request.type}")
    @DeleteMapping("/{id}")
    public Result<Dict> removeDict(@PathVariable Long id) {
        log.debug("API CALL : remove dict.");

        return ok(dictService.removeDict(id));
    }
}
